package cn.edu.stu.syllabus.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import butterknife.ButterKnife;
import cn.edu.stu.syllabus.R;

/**
 * Created by daidaijie on 2017/4/13.
 */

public abstract class BaseDialog extends Dialog {

    protected Activity mActivity;

    public BaseDialog(Context context) {
        super(context, R.style.DialogStyle);
        mActivity = (Activity) context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(getContentViewId());

        initWindow();

        ButterKnife.bind(this);
        initView(savedInstanceState);
    }

    protected abstract void initView(Bundle savedInstanceState);

    @LayoutRes
    protected abstract int getContentViewId();

    protected void initWindow() {
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);

        int devideHeight = mActivity.getWindowManager().getDefaultDisplay().getHeight();
        devideHeight -= getStatusBarHeight();

        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = devideHeight;
        lp.gravity = Gravity.BOTTOM;
        win.setAttributes(lp);
        win.getAttributes().windowAnimations = R.style.PauseDialogAnimation;
    }

    protected int getStatusBarHeight() {
        int result = 0;
        int resourceId = getContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getContext().getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }


}
